package cn.hs.controller;

import cn.hs.entity.Article;
import cn.hs.service.ArticleService;
import cn.hs.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClassName ArticleController
 * @Description TODO
 * @Author CnYuu
 * @Date 2024年10月22日 20:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @GetMapping("/find")
    public Object find(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            Article article
    ){
        PageInfo pageInfo = articleService.findAll(pageNum, pageSize,article);
        return  Result.success(pageInfo);
    }

    @PostMapping("/insert")
    public Object insert(@RequestBody Article article){
        article.setCreatetime(new Date());
        int insert = articleService.insert(article);
        if(insert!=0){
            return Result.success("添加成功");
        }else {
            return Result.error("添加失败");
        }
    }
    @PostMapping("/update")
    public Object update(@RequestBody Article article){
        article.setCreatetime(new Date());
        int update = articleService.update(article);
        if(update!=0){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    @PostMapping("/delete")
    public Object delete(@RequestBody Article article){
        int delete= articleService.delete(article.getId());
        if(delete!=0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
}
